﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Telerik.Web.UI;
using Medianamik.Core;
using System.Web.UI.HtmlControls;

namespace Medianamik.UI.TypeManagement.Controls
{
    public class TreeViewTemplate : ITemplate
    {
        private readonly RadTreeView _tree = new RadTreeView();

        public void InstantiateIn(Control container)
        {
            var div = new HtmlGenericControl("div");
            div.Attributes.Add("onclick", "StopPropagation(event)");
            container.Controls.Add(div);
            div.Controls.Add(Tree);

            Tree.Nodes.AddRange(ConfigManager.GetAll<MedianamikModule>()
            .OrderBy(m => m.Position).Select(m => new RadTreeNode(m.DisplayName, m.ID.ToString())));

            foreach (RadTreeNode node in Tree.Nodes)
            {
                node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            }

            Tree.NodeExpand += NodeExpand;
            
            var specializedScriptName = "OnClientNodeClickingHandlerFor" + container.Parent.ClientID;
            
            Tree.OnClientNodeClicking = specializedScriptName;

            //Le script doit exister (différent) pour chaque instance de ComboTree
            var specializedScript = String.Concat("function "+ specializedScriptName + "(sender, args) {\n",
                    " var comboBox = $find('" + container.Parent.ClientID + "');\n",
                    " var node = args.get_node();\n",
                    " var comboBoxItem = comboBox.get_items().getItem(0);\n",
                    "if (node.get_category() == \"Disabled\" || node.get_value() == comboBoxItem.get_value()) {\n",
                        "args.set_cancel(true);}\n",
                    "else {\n",
                        " comboBox.trackChanges();\n",
                        " comboBoxItem.set_value(node.get_value());\n",
                        " comboBox.set_text(node.get_text());\n",
                        " comboBox.commitChanges();\n",
                        " comboBox.hideDropDown();}\n",
                "}\n");

            var genericScript = String.Concat("function StopPropagation(e) {\n",
                    "if(typeof(e) == 'undefined' || !e) {\n",
                        " e = window.event;}\n",
                    "e.cancelBubble = true;\n",
                "}\n");

            ScriptManager.RegisterClientScriptBlock(container.Page, typeof(Page), specializedScriptName, specializedScript, true);
            ScriptManager.RegisterClientScriptBlock(container.Page, typeof(Page), "StopPropagation", genericScript, true);
        }

        private static void NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            ExpandNode(e.Node);
        }

        public static void ExpandNode(RadTreeNode node)
        {
            node.Expanded = true;

            var medNodes = NodeManager.GetChildNodes(new Guid(node.Value));

            foreach (var n in medNodes)
	        {
		         var treeNode = new RadTreeNode(Utility.GetFriendlyIdentifier(n, 
                     MedianamikContext.Current.CurrentUICulture.Name),
                            n.ID.ToString());

                if(n.HasChildren)
                    treeNode.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;

                node.Nodes.Add(treeNode);
	        }
        }

        public RadTreeView Tree
        {
            get { return _tree; }
        }
    }
}
